/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * EditorRenderer.java
 *
 * Created on 09.04.2009, 22:53:23
 */
package de.tor.util;

import de.tor.util.renderer.EditorRendererRenderer;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.dnd.DropTarget;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;

/**
 *
 * @author Charon
 */
public class EditorRenderer extends javax.swing.JPanel{

    private BufferedImage mBuffer = null;
    private static EditorRenderer SINGLETON = null;
    private EditorRendererRenderer mRenderer = null;
    private Point mLastMousePos = null;

    public static synchronized EditorRenderer getSingleton() {
        if (SINGLETON == null) {
            SINGLETON = new EditorRenderer();
        }
        return SINGLETON;
    }

    /** Creates new form EditorRenderer */
    EditorRenderer() {
        initComponents();
        mRenderer = new EditorRendererRenderer();
        mRenderer.setDaemon(true);
        mRenderer.start();

        addMouseListener(new MouseListener() {

            public void mouseClicked(MouseEvent e) {
            }

            public void mousePressed(MouseEvent e) {
                mLastMousePos = e.getPoint();
            }

            public void mouseReleased(MouseEvent e) {
                mLastMousePos = null;
            }

            public void mouseEntered(MouseEvent e) {
            }

            public void mouseExited(MouseEvent e) {
            }
        });

        addMouseMotionListener(new MouseMotionListener() {

            public void mouseDragged(MouseEvent e) {
                Point currentPos = e.getPoint();
                getRenderer().moveVirtualPosition((int) (mLastMousePos.getX() - currentPos.getX()), (int) (mLastMousePos.getY() - currentPos.getY()));
                mLastMousePos.move(currentPos.x, currentPos.y);
            }

            public void mouseMoved(MouseEvent e) {
            }
        });
    }

    public EditorRendererRenderer getRenderer() {
        return mRenderer;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents

    public void paint(Graphics g) {
        if (mBuffer == null) {
            super.paint(g);
        } else {
            g.drawImage(mBuffer, 0, 0, this);
        }
    }

    public void updateBuffer(BufferedImage pBuffer) {
        mBuffer = pBuffer;
        repaint();
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
}
